package com.itita.ww2.utils.handler;

import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.itita.ww2.core.WW2Exception;

public class MultiHandlerUtil {
	private static final Logger logger = LoggerFactory.getLogger(MultiHandlerUtil.class);
	/**
	 * 表驱动+接口+反射 消除繁琐的逻辑判断语句,强化 "OCP" (开闭原则)
	 */
	private ConcurrentHashMap<String, Object> requestMap;

	public MultiHandlerUtil() {
		this.requestMap = new ConcurrentHashMap<>();
	}

	public void execute(String requestId, String requestCMD, User user, ISFSObject params)
			throws WW2Exception, SQLException {
		// Way 2): Query interface from Drive-Table and use it!
		// IRequestOper iaro = null;
		// try {
		// iaro = requests.get(parent).get(requestId);
		// } catch (NullPointerException e) {
		// // Check if "key == null"
		// ext.trace("requestId: " + requestId + " not found!");
		// e.printStackTrace();
		// return;
		// }
		// // Check if "value == null"
		// if (iaro == null) {
		// return;
		// }

		// Way 3): Execute
		if (!requestMap.containsKey(requestId)) {
			logger.trace("No Such request instance:" + requestId);
			return;
		}

		Object obj = requestMap.get(requestId);
		if (obj instanceof IRequestOper) {
			((IRequestOper) obj).execute(requestCMD, user, params);
		}
	}

	public void init(BaseClientRequestHandler handler, String requestId, Object[] objs) {
		// Way 3): Execute
		if (!requestMap.containsKey(requestId)) {
			handler.getParentExtension().trace("No Such request instance:" + requestId);
			return;
		}

		Object obj = requestMap.get(requestId);
		if (obj instanceof IRequestData) {
			((IRequestData) obj).init(objs);
		}
	}

	/**
	 * 驱动表中加入项
	 * 
	 * @param requestId
	 *            RequestId
	 * @param theClass
	 */
	public void addClientRequestItem(String requestId, Class<?> theClass) {
		if (theClass == null || requestId == null) {
			return;
		}
		if (requestId.equals("")) {
			return;
		}

		// // Way 1): generic interface
		// IRequestOper iro = null;
		// try {
		// iro = (IRequestOper) theClass.newInstance(); // use default
		// // constructor
		// } catch (InstantiationException | IllegalAccessException e) {
		// // Do nothing, just catch it!
		// e.printStackTrace();
		// }
		// // double check!
		// if (iro == null) {
		// return;
		// }
		// // Way 1): put the interface into the HashMap
		// ConcurrentHashMap<String, IRequestOper> map = null;
		// if (!requests.containsKey(parent)) {
		// map = new ConcurrentHashMap<>();
		// requests.put(parent, map);
		// } else {
		// map = requests.get(parent);
		// }
		// // put!
		// map.put(requestId, iro);

		// requestMap.put(requestId, iro);

		// Way 2): put theClass into the HashMap
		try {
			requestMap.put(requestId, theClass.newInstance());
		} catch (InstantiationException | IllegalAccessException e) {
			// Just Catch it , Do nothing...
			e.printStackTrace();
		}
	}

	// /**
	// * 驱动表中加入项
	// *
	// * @param parent
	// * @param requestId
	// * @param oper
	// */
	// public void addClientRequestItem(Class<?> parent, String requestId,
	// IRequestOper iro) {
	// if (parent == null || requestId == null || requestId.equals("") || iro ==
	// null) {
	// return;
	// }
	// // Way 1): put the interface into the HashMap
	// ConcurrentHashMap<String, IRequestOper> map = null;
	// if (!requests.containsKey(parent)) {
	// map = new ConcurrentHashMap<>();
	// requests.put(parent, map);
	// } else {
	// map = requests.get(parent);
	// }
	// // put!
	// map.put(requestId, iro);
	// }
}
